/**
 * 数据操作类
 */

const cookie = {
  /**
   * @description 设置Cookie
   * @param { String } name cookie名称
   * @param { String } value cooke的值
   * @param { Number } exp 过期时间 默认2小时 单位毫秒
   * @link https://ifmiss.github.io/d-js-utils/#/lib/_cookie?id=setcookie
   * @example
   * // 设置name为test的值为12345，设置过期时间为1小时
   * Dutils.cookie.setCookie('test', '12345', 60 * 60 * 1000)
   */
  setCookie(name, value, exp = 60 * 60 * 24 * 1000 * 15) {
    let date = new Date()
    date.setTime(date.getTime() + exp)
    document.cookie = `${name}=${escape(value)};expires=${date.toGMTString()}`
  },

  /**
   * @description 获取Cookie
   * @param { String } name cookie名称
   * @returns { (Array | Null) } 返回数据
   * @link https://ifmiss.github.io/d-js-utils/#/lib/_cookie?id=getcookie
   * @example
   * Dutils.cookie.getCookie('test')
   */
  // getCookie(name) {
  //   if (name) {
  //     let reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`)
  //     let arr = document.cookie.match(reg)
  //     return arr && arr[2] ? arr[2] : null
  //   }
  //   let getAllCookies = []
  //   if (document.cookie !== '') {
  //     let arrCookie = document.cookie.split('; ')
  //     for (let k in arrCookie) {
  //       getAllCookies.push({
  //         name: `${unescape(arrCookie[k].split('=')[0])}`,
  //         value: `${unescape(arrCookie[k].split('=')[1])}`
  //       })
  //     }
  //     return getAllCookies
  //   } else {
  //     return null
  //   }
  // },
  // getCookie(cookieName, stringCookie) {
  //   let strCookie = new RegExp('' + cookieName + '[^;]+').exec(stringCookie)[0]
  //   return unescape(strCookie ? strCookie.toString().replace(/^[^=]+./, '') : '')
  // },

  /**
   * @description 删除Cookie
   * @param { String } name cookie名称 如果不传参数则设置所有cookie过期
   * @link https://ifmiss.github.io/d-js-utils/#/lib/_cookie?id=rmcookie
   * @example
   * Dutils.cookie.rmCookie('test')
   */
  rmCookie(name) {
    let date = new Date()
    date.setTime(date.getTime() - 1)
    if (name) {
      let cookieInfo = cookie.getCookie(name)
      if (cookieInfo !== null) {
        document.cookie = `${name}=${cookieInfo};expires=${date.toGMTString()}`
      }
      return
    }
    let getAllCookies = cookie.getCookie()
    for (let k in getAllCookies) {
      document.cookie = `${getAllCookies[k].name}=${getAllCookies[k].value};expires=${date.toGMTString()}`
    }
  }

  // setStorage(name, value) {
  //   // cookie.isStorage()
  //   localStorage.setItem(key, JSON.stringify(value))
  // },
  // getStorage(key) {
  //   // cookie.isStorage()
  //   return JSON.parse(localStorage.getItem(key))
  // },
  // removeStorage(key) {
  //   localStorage.removeItem(key)
  // },
  // isStorage() {
  //   if (!localStorage) {
  //     console.log('你的浏览器不支持localStorage')
  //     return false
  //   }
  // }

}
export default cookie
